/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.dao;

import hibernate.entity.Event;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Neo
 */
public class EventDAO extends AbstractDAO {

    public List<Event> readAll() {
        query = session.createQuery("from Event");
        return query.list();
    }

    public Event readByID(int id) {
        query = session.createQuery("from Events where eventId =:id");
        query.setParameter("id", id);
        return (Event) query.list().get(0);
    }

    public int create(Event s) {
        try {
            tx = session.beginTransaction();
            session.save(s);
            tx.commit();
            return 1;
        } catch (Exception e) {
            tx.rollback();
            return 0;
        }
    }

    public int delete(Event s) {
        try {
            tx = session.beginTransaction();
            session.delete(s);
            tx.commit();
            return 1;
        } catch (Exception e) {
            tx.rollback();
            return 0;
        }
    }

    public int update(Event s) {
        try {
            tx = session.beginTransaction();
            session.saveOrUpdate(s);
            tx.commit();
            return 1;
        } catch (Exception e) {
            tx.rollback();
            return 0;
        }
    }

    public List<Event> readByDays(int num) {
        Calendar c = Calendar.getInstance();
        c.add(1, num);
        SimpleDateFormat s = new SimpleDateFormat();
        Date d = c.getTime();
        query = session.createQuery("from Events where startDate < :d ");
        query.setParameter("d", d);
        return query.list();

    }
    public static void main(String[] args) {
        new EventDAO().readByDays(3);
    }
}
